package com.adopapa.rabbitdemo;

import java.util.Date;
import java.util.UUID;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class HelloSender implements RabbitTemplate.ConfirmCallback, ApplicationListener<ApplicationReadyEvent> {

	@Autowired
	private RabbitTemplate rabbitTemplate;
	// private AmqpTemplate rabbitTemplate;
	
	public void send() {
		String content = "北京 " + new Date();
		System.out.println("sender: " + content);
		CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
		this.rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE, RabbitConfig.ROUTINGKEY, content, correlationData);
	}

	@Override
	public void confirm(CorrelationData correlationData, boolean ack, String cause) {
		System.out.println(correlationData);
		System.err.println(ack);
		System.err.println(cause);

	}

	@Override
	public void onApplicationEvent(ApplicationReadyEvent event) {
		this.rabbitTemplate.setConfirmCallback(this);
		
	}

}
